package com.icow.wms.basic.service.impl;

import java.util.List;

import com.icow.wms.basic.domain.TransportOrder;
import com.icow.wms.basic.domain.WcsTaskState;
import com.icow.wms.basic.utils.CallbackMoment;
import com.icow.wms.basic.utils.JsonAssignment;
import com.icow.wms.basic.utils.OpenTcs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icow.wms.basic.mapper.CallbackInterfaceMapper;
import com.icow.wms.basic.domain.CallbackInterface;
import com.icow.wms.basic.service.ICallbackInterfaceService;

/**
 * callbackService业务层处理
 *
 * @author bcs
 * @date 2024-09-19
 */
@Service
public class CallbackInterfaceServiceImpl implements ICallbackInterfaceService {
    @Autowired
    private CallbackInterfaceMapper callbackInterfaceMapper;

    /**
     * 查询callback
     *
     * @param id callbackID
     * @return callback
     */
    @Override
    public CallbackInterface selectCallbackInterfaceById(Long id) {
        return callbackInterfaceMapper.selectCallbackInterfaceById(id);
    }

    @Override
    public void callbackThreadBoot(String callbackMoment, String agvName, TransportOrder transportOrder) {
        CallbackInterface callbackInterface = new CallbackInterface();
        callbackInterface.setCallbackMoment(callbackMoment);
        List<CallbackInterface> callbackInterfaces = callbackInterfaceMapper.selectCallbackInterfaceList(callbackInterface);
        if (!callbackInterfaces.isEmpty()) {
            for (CallbackInterface callback : callbackInterfaces) {
                String json = JsonAssignment.getJson(callback.getCallbackJson(), transportOrder.getName(),
                        Integer.parseInt(agvName.substring(agvName.length() - 4)), agvName,
                        OpenTcs.getLoadLocation(transportOrder), OpenTcs.getUnloadLocation(transportOrder));
                WcsTaskState wcsTaskState = new WcsTaskState(callback.getCallbackUrl(), callback.getCallbackToken(), json);
                new Thread(wcsTaskState).start();
            }
        }
    }

    /**
     * 查询callback列表
     *
     * @param callbackInterface callback
     * @return callback
     */
    @Override
    public List<CallbackInterface> selectCallbackInterfaceList(CallbackInterface callbackInterface) {
        return callbackInterfaceMapper.selectCallbackInterfaceList(callbackInterface);
    }

    /**
     * 新增callback
     *
     * @param callbackInterface callback
     * @return 结果
     */
    @Override
    public int insertCallbackInterface(CallbackInterface callbackInterface) {
        return callbackInterfaceMapper.insertCallbackInterface(callbackInterface);
    }

    /**
     * 修改callback
     *
     * @param callbackInterface callback
     * @return 结果
     */
    @Override
    public int updateCallbackInterface(CallbackInterface callbackInterface) {
        return callbackInterfaceMapper.updateCallbackInterface(callbackInterface);
    }

    /**
     * 批量删除callback
     *
     * @param ids 需要删除的callbackID
     * @return 结果
     */
    @Override
    public int deleteCallbackInterfaceByIds(Long[] ids) {
        return callbackInterfaceMapper.deleteCallbackInterfaceByIds(ids);
    }

    /**
     * 删除callback信息
     *
     * @param id callbackID
     * @return 结果
     */
    @Override
    public int deleteCallbackInterfaceById(Long id) {
        return callbackInterfaceMapper.deleteCallbackInterfaceById(id);
    }
}
